﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace SPattern.BasicClass
{
    /// <summary>
    /// ItemSet(項目集): 由好幾Item組成(List)的集合
    /// </summary>
    [Serializable]
    public class ItemSet : Collection<Item>
    {
        /// <summary>
        /// ItemSet之編號
        /// </summary>
        // public string Id { get; set; }

        /// <summary>
        /// 轉換後對應之編號
        /// </summary>
        public string MappedId { get; set; }

        /// <summary>
        /// 交易發生時間編號
        /// </summary>
        public int TimeID { get; set; }

        public ItemSet()
        {
            TimeID = -1;
        }

        public ItemSet(ItemSet itemSet):base()
        {
            foreach (Item item in itemSet)
                this.Add(new Item(item.Id));
            this.MappedId = itemSet.MappedId;
        }
        /// <summary>
        /// 比較兩項目集的Id是否相同
        /// </summary>
        /// <param name="set">輸入項目集</param>
        /// <returns>true:兩者相同, fasle: 兩者不同</returns>
        /// 
        /*
        public bool EqualsId(ItemSet set)
        {
            return this.Id == set.Id;
        }
        */
        /// <summary>
        /// 比較兩項目集的MappedId是否相同
        /// </summary>
        /// <param name="set">輸入項目集</param>
        /// <returns>true:兩者相同, fasle: 兩者不同</returns>
        public bool EqualsMappedId(ItemSet set)
        {
            return this.MappedId == set.MappedId;
        }

        /// <summary>
        /// 比較兩項目集是否完全相同
        /// </summary>
        /// <param name="set">輸入項目集</param>
        /// <returns>true:兩者相同, fasle: 兩者不同</returns>
        public bool EqualsTotal(object obj)
        {
            return ((ItemSet)obj).ToString() == this.ToString();
        }

        /// <summary>
        /// 判斷是否包含某個項目集
        /// </summary>
        /// <param name="set">輸入的項目集</param>
        /// <returns></returns>
        public bool Contains(ItemSet set)
        {
            foreach (Item I in set)
            {
                if (!this.Contains(I)) return false;
            }
            return true;
        }

        public override string ToString()
        {
            string msg = "(";
            List<string> tmp = new List<string>();
            foreach (Item item in this)
                tmp.Add(item.Id);
            msg += String.Join(",", tmp.ToArray());
            msg += ")";
            return msg;
        }

        public void Sort()
        {
            IEnumerable<Item> item = this.OrderBy<Item, string>(i => i.Id);

            int idx=0;
            foreach (Item it in item)
                this[idx++] = it;
        }
    }
}
